package com.java01.jdbc.statement;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class PrepareStatement1 {

	public static void main(String[] args) throws Exception {
		Class.forName("org.sqlite.JDBC");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:buddy.db");
		Statement stat = conn.createStatement();
		stat.executeUpdate("drop table if exists friend;");
		stat.executeUpdate("create table friend (name, nickname);");
		PreparedStatement prep = conn.prepareStatement("insert into friend values (?, ?);");

		prep.setString(1, "atoo");
		prep.setString(2, "Great Atoo");
		prep.addBatch();
		prep.setString(1, "xxkuo");
		prep.setString(2, "KSong Lover");
		prep.addBatch();
		prep.setString(1, "sj");
		prep.setString(2, "Same Jack");
		prep.addBatch();

		conn.setAutoCommit(false);
		prep.executeBatch();
		conn.setAutoCommit(true);

		ResultSet rs = stat.executeQuery("select * from friend;");
		while (rs.next()) {
			System.out.println(rs.getString("name")+"'s nick name is '"+rs.getString("nickname")+"'");
		}
		rs.close();
		conn.close();
	}
}
